package com.acme.chat.message;

import java.io.Serializable;

/**
 * Class represents message object by which server and client exchange info.
 */
public class ChatMessage implements Message, Serializable {
    private MessageType type;
    private String message;
    private String nameToWho;
    private String room;

    /**
     * Create Message with specified type and string without trailing spaces.
     *
     * @param type
     * @param message
     * @param nameToWho
     * @param room
     */
    private ChatMessage(final MessageType type, final String message, final String nameToWho, final String room) {
        this.type = type;
        this.message = deleteTrailingSpaces(message);
        if (nameToWho != null) {
            this.nameToWho = nameToWho.trim();
        }
        this.room=room;
    }

    /**
     * Create Message with type and empty string.
     *
     * @param type
     */
    public ChatMessage(final MessageType type) {
        this(type, null, null, null);
    }

    /**
     * Create Message with type and message string.
     *
     * @param type
     * @param message
     */
    public ChatMessage(final MessageType type, final String message) {
        this(type, message, null, null);
    }

    /**
     * Create Message with type, message string and user name.
     * Used for private messages.
     *
     * @param message
     * @param nameToWho
     */
    public ChatMessage(final String message, final String nameToWho) {
        this(MessageType.PRIVATE, message, nameToWho, null);
    }

    /**
     * Create Message with type, message string and user name.
     * Used for private messages.
     *
     * @param room
     */
    public ChatMessage(final String room) {
        this(MessageType.CHANGE_ROOM, null, null, room);
    }

    /**
     * Method deletes trailing spaces from the string and always add new line symbol.
     *
     * @param messageText
     * @return String
     */
    private String deleteTrailingSpaces(String messageText) {
        if (messageText == null) { return ""; }
        StringBuilder sb = new StringBuilder(messageText);
        for (int i = messageText.length() - 1; i >= 0; i--) {
            if (Character.isWhitespace(sb.charAt(i))) {
                sb.deleteCharAt(i);
            } else {
                break;
            }
        }
        if (!sb.toString().trim().isEmpty()) {
            sb.append("\n");
        }
        return sb.toString();
    }

    @Override
    public MessageType getType() {
        return type;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public String getNameToWho() {
        return nameToWho;
    }

    @Override
    public String getRoom() {
        return room;
    }

    @Override
    public String toString() {
        return "ChatMessage [type=" + type + ", message=" + message + ", nameToWho=" + nameToWho + ", room=" + room + "]";
    }
}
